package org.wk.core.common.http;

import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
* @ClassName	: LBSRequestHttps
* @Package		：com.lbs.core.http
* @Author mars mars@labsport.cn
* @Create Time	：2016年8月27日 下午12:35:47
* @Description	: https请求
*/ 
public class LBSRequestHttps extends LBSRequestBase {
	private int maxTotal = 200;
	private int maxPerRoute = 40;
	private int maxRoute = 100;
	private int timeOut = 10 * 1000;
	private Map<String, String> headers;
	private HttpEntity requestEntity;
	private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = null;
	
	protected LBSRequestHttps(SSLContext sslContext) {
		LayeredConnectionSocketFactory layeredConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext);
		Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
				.register("http", PlainConnectionSocketFactory.getSocketFactory())
				.register("https", layeredConnectionSocketFactory).build();
		poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
	}

	@Override
	public CloseableHttpClient getHttpClient(String hostname, int port) {
		poolingHttpClientConnectionManager.setMaxTotal(maxTotal);
		poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
		HttpHost httpHost = new HttpHost(hostname, port);
		poolingHttpClientConnectionManager.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager)
				.setRetryHandler(this).build();
		return httpClient;
	}
	@Override
	public void config(HttpRequestBase httpRequestBase) {
		if (headers != null) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				String value = entry.getValue();
				String key = entry.getKey();
				httpRequestBase.setHeader(key, value);
			}
		}
		if (requestEntity != null && httpRequestBase instanceof HttpPost) {
			((HttpPost) httpRequestBase).setEntity(requestEntity);
		}
		// 配置请求的超时设置
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectionRequestTimeout(timeOut)
				.setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
		httpRequestBase.setConfig(requestConfig);
	}

	@Override
	public LBSRequest setMaxTotal(int maxTotal) {
		// TODO Auto-generated method stub
		this.maxTotal = maxTotal;
		return this;
	}
	@Override
	public LBSRequest setMaxPerRoute(int maxPerRoute) {
		// TODO Auto-generated method stub
		this.maxPerRoute = maxPerRoute;
		return this;
	}
	@Override
	public LBSRequest setMaxRoute(int maxRoute) {
		// TODO Auto-generated method stub
		this.maxRoute = maxRoute;
		return this;
	}
	@Override
	public LBSRequest setTimeOut(int timeOut) {
		// TODO Auto-generated method stub
		this.timeOut = timeOut;
		return this;
	}
	@Override
	public LBSRequest setHeaders(Map<String, String> headers) {
		// TODO Auto-generated method stub
		this.headers = headers;
		return this;
	}
}
